package com.eh.iteratorAndComposition.modle;

import com.eh.iteratorAndComposition.base.AbstractMenu;
import com.eh.iteratorAndComposition.base.MenuComponent;
import com.eh.iteratorAndComposition.iterator.ArrayIterator;

import java.util.Arrays;
import java.util.Iterator;

/**
 * Created by David on 2016/5/1.
 */
public class DinnerMenu extends AbstractMenu {
    private static int Dinner_Menu_Size = 20;
    private MenuComponent[] menus;
    private int index;

    public DinnerMenu() {
        name = "午餐菜单";
        description = "种类丰富， 机具营养";
        menus = new MenuComponent[Dinner_Menu_Size];
        index = 0;
        add(new MenuItem("猪肉披萨", "好吃的披萨", 22.5, false));
        add(new MenuItem("青菜披萨", "零脂肪，爱美人士的首选", 15.7, true));
        DesertMenu desertMenu = new DesertMenu();
        add(desertMenu);
    }

    @Override
    protected Iterator<MenuComponent> createIterator() {
        return new ArrayIterator(menus);
    }

    @Override
    public void add(MenuComponent menuComponent) {
        if (index >= Dinner_Menu_Size) {
            menus = Arrays.copyOf(menus, Dinner_Menu_Size * 2);
        }
        menus[index++] = menuComponent;
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        Iterator<MenuComponent> iterator = createIterator();
        while (iterator.hasNext()) {
            MenuComponent m = iterator.next();
            if (equals(m, menuComponent)) {
                iterator.remove();
            }
        }
    }

    @Override
    public MenuComponent get(int i) {
        return menus[i];
    }

    private boolean equals(MenuComponent m1, MenuComponent m2) {
        return m1.getName().equals(m2.getName());
    }
}
